package com.snail.billing.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.net.HttpApp;
import com.android.net.HttpConst;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.os.Page;
import com.android.util.AlertUtil;
import com.android.util.ResUtil;
import com.snail.billing.DataCache;
import com.snail.billing.Resource;
import com.snail.billing.data.AccountManager;
import com.snail.billing.json.JsonBase;
import com.snail.billing.json.JsonUpdatePwd;
import com.snail.billing.session.ResetPasswordEmailSession;
import com.snail.billing.session.ResetPasswordSession;
import com.snail.billing.session.SendEmailTokenSession;
import com.snail.billing.session.SendSmsCodeSession;
import com.snail.billing.session.UpdatePwdSession;
import com.snail.billing.util.BillingUtil;

public class PasswordFragment extends Page implements View.OnClickListener, OnHttpResultListener {

	public static final byte MODIFY_PWD = 0;
	public static final byte MODIFY_PWD_RANDOM = 1;
	public static final byte FORGET_PWD_MOBILE = 2;
	public static final byte FORGET_PWD_EMAIL = 3;

	private TextView textTitle;
	private View buttonBack;

	private EditText inputAccount;
	private EditText inputOldPwd;
	private EditText inputPwd1;
	private EditText inputPwd2;
	private EditText inputCaptcha;
	private View buttonSend;
	private View button;

	private UpdatePwdSession updatePwdSession;
	private SendSmsCodeSession sendSmsCodeSession;
	private SendEmailTokenSession sendEmailTokenSession;
	private ResetPasswordSession resetPasswordSession;
	private ResetPasswordEmailSession resetPasswordEmailSession;

	private int state;
	private String account;
	private String captcha;
	private String oldPwd;
	private String pwd;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_password_activity);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		state = getPageArgs().getInt("state");
		account = getPageArgs().getString("account");
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		if (state == MODIFY_PWD) {
			textTitle.setText(ResUtil.getString(Resource.string.snailbilling_password_title));
		} else if (state == MODIFY_PWD_RANDOM) {
			oldPwd = getPageArgs().getString("pwd");
			textTitle.setText(ResUtil.getString(Resource.string.snailbilling_password_title));
		} else if (state == FORGET_PWD_MOBILE || state == FORGET_PWD_EMAIL) {
			textTitle.setText(ResUtil.getString(Resource.string.snailbilling_forget_title));
		}
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		inputAccount = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_password_input_account));
		inputAccount.setText(account);
		inputAccount.setEnabled(false);
		inputOldPwd = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_password_input_old_pwd));
		inputPwd1 = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_password_input_pwd1));
		inputPwd2 = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_password_input_pwd2));
		inputCaptcha = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_password_input_captcha));
		buttonSend = findViewById(ResUtil.getViewId(Resource.id.snailbilling_password_button_send));
		buttonSend.setOnClickListener(this);
		button = findViewById(ResUtil.getViewId(Resource.id.snailbilling_password_button));
		button.setOnClickListener(this);
		if (state == MODIFY_PWD_RANDOM || state == FORGET_PWD_MOBILE || state == FORGET_PWD_EMAIL) {
			inputOldPwd.setVisibility(View.GONE);
		}
		if (state == MODIFY_PWD || state == MODIFY_PWD_RANDOM) {
			inputCaptcha.setVisibility(View.GONE);
			buttonSend.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			getPageManager().backward();
		} else if (v.equals(buttonSend)) {
			if (state == FORGET_PWD_MOBILE) {
				requestSendSmsCode();
			} else if (state == FORGET_PWD_EMAIL) {
				requestSendEmailToken();
			}
		} else if (v.equals(button)) {
			if (state == MODIFY_PWD) {
				oldPwd = inputOldPwd.getText().toString();
				if (TextUtils.isEmpty(oldPwd)) {
					AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_password_input_old_pwd));
					return;
				}
			}
			pwd = inputPwd1.getText().toString();
			if (TextUtils.isEmpty(pwd)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_password_input_pwd1));
				return;
			}
			String pwd2 = inputPwd2.getText().toString();
			if (TextUtils.isEmpty(pwd2)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_password_input_pwd2));
				return;
			}
			if (!TextUtils.equals(pwd, pwd2)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_password_pwd_not_same));
				return;
			}
			if (TextUtils.equals(oldPwd, pwd)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_password_pwd_same_than_old));
				return;
			}
			if (pwd.length() < 6) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_password_pwd_must_more_than_six));
				return;
			}
			if (!BillingUtil.validatePwd(pwd)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_password_pwd_validate));
				return;
			}
			if (state == FORGET_PWD_MOBILE || state == FORGET_PWD_EMAIL) {
				captcha = inputCaptcha.getText().toString();
				if (TextUtils.isEmpty(captcha)) {
					AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_password_input_captcha));
					return;
				}
			}
			if (state == MODIFY_PWD || state == MODIFY_PWD_RANDOM) {
				requestUpdatePwd();
			} else if (state == FORGET_PWD_MOBILE) {
				requestResetPassword();
			} else if (state == FORGET_PWD_EMAIL) {
				requestResetPasswordEmail();
			}
		}
	}

	private void requestUpdatePwd() {
		HttpApp httpApp = new HttpApp(getContext());
		updatePwdSession = new UpdatePwdSession(oldPwd, pwd);
		httpApp.setOnHttpResultListener(this);
		httpApp.request(updatePwdSession);
	}

	private void requestSendSmsCode() {
		HttpApp httpApp = new HttpApp(getContext());
		sendSmsCodeSession = new SendSmsCodeSession(account);
		httpApp.setOnHttpResultListener(this);
		httpApp.request(sendSmsCodeSession);
	}

	private void requestSendEmailToken() {
		HttpApp httpApp = new HttpApp(getContext());
		sendEmailTokenSession = new SendEmailTokenSession(account);
		httpApp.setOnHttpResultListener(this);
		httpApp.request(sendEmailTokenSession);
	}

	private void requestResetPassword() {
		HttpApp httpApp = new HttpApp(getContext());
		resetPasswordSession = new ResetPasswordSession(account, captcha, pwd);
		httpApp.setOnHttpResultListener(this);
		httpApp.request(resetPasswordSession);
	}

	private void requestResetPasswordEmail() {
		HttpApp httpApp = new HttpApp(getContext());
		resetPasswordEmailSession = new ResetPasswordEmailSession(account, captcha, pwd);
		httpApp.setOnHttpResultListener(this);
		httpApp.request(resetPasswordEmailSession);
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(updatePwdSession)) {
				JsonUpdatePwd jsonUpdatePwd = new JsonUpdatePwd((String) updatePwdSession.getResponseData());
				if (!TextUtils.isEmpty(jsonUpdatePwd.getSessionId())) {
					DataCache.getInstance().importParams.sessionId = jsonUpdatePwd.getSessionId();
				}
				if (jsonUpdatePwd.getCode() == 1) {
					AccountManager.saveAccount(account, pwd);
					AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
					builder.setMessage(ResUtil.getString(Resource.string.snailbilling_password_pwd_ok));
					builder.setPositiveButton(ResUtil.getString(HttpConst.string.NET_OK), new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							getPageManager().backward();
						}
					});
					builder.show();
				} else {
					if (!TextUtils.isEmpty(jsonUpdatePwd.getErrorMsg())) {
						Toast.makeText(getContext(), jsonUpdatePwd.getErrorMsg(), Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(getContext(), jsonUpdatePwd.getMessage(), Toast.LENGTH_SHORT).show();
					}
				}
			} else if (httpSession.equals(sendSmsCodeSession)) {
				JsonBase jsonBase = new JsonBase((String) sendSmsCodeSession.getResponseData());
				Toast.makeText(getContext(), jsonBase.getMessage(), Toast.LENGTH_SHORT).show();
			} else if (httpSession.equals(sendEmailTokenSession)) {
				JsonBase jsonBase = new JsonBase((String) sendEmailTokenSession.getResponseData());
				Toast.makeText(getContext(), jsonBase.getMessage(), Toast.LENGTH_SHORT).show();
			} else if (httpSession.equals(resetPasswordSession)) {
				JsonBase jsonBase = new JsonBase((String) resetPasswordSession.getResponseData());
				Toast.makeText(getContext(), jsonBase.getMessage(), Toast.LENGTH_SHORT).show();
				if (jsonBase.getCode() == 1) {
					AccountManager.saveAccount(account, pwd);
					getPageManager().backward(1);
				}
			} else if (httpSession.equals(resetPasswordEmailSession)) {
				JsonBase jsonBase = new JsonBase((String) resetPasswordEmailSession.getResponseData());
				Toast.makeText(getContext(), jsonBase.getMessage(), Toast.LENGTH_SHORT).show();
				if (jsonBase.getCode() == 1) {
					AccountManager.saveAccount(account, pwd);
					getPageManager().backward(1);
				}
			}
		}
	}

}
